package cn.jinhe.doc.view.action;

import java.util.Date;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.jinhe.doc.base.BaseAction;
import cn.jinhe.doc.domain.Reply;
import cn.jinhe.doc.domain.Repository;
import cn.jinhe.doc.domain.Topic;
import cn.jinhe.doc.util.QueryHelper;

@Controller
@Scope("prototype")
public class TopicAction extends BaseAction<Topic> {

	private Long repositoryId;

	private Long topicId;
	
	public Long getTopicId() {
		return topicId;
	}

	public void setTopicId(Long topicId) {
		this.topicId = topicId;
	}

	public String show() throws Exception {
		// 准备数据:topic
		Topic topic = topicService.getById(model.getId());
		ActionContext.getContext().put("topic", topic);

		// 准备数据:replyList
		// List<Reply> replyList=replyService.findByTopic(topic);
		// ActionContext.getContext().put("replyList", replyList);

		// 准备分页信息

		// PageBean pageBean = replyService.getPageBeanByTopic(pageNum,
		// pageSize, topic);
		// ActionContext.getContext().getValueStack().push(pageBean);

		// String hql = "FROM Reply r WHERE r.topic=? ORDER BY r.postTime ASC";
		// String hqlCount = "FROM Reply r WHERE r.topic=? ";
		//
		// List<Object> parameters = new ArrayList<Object>();
		// parameters.add(topic);
		//
		// PageBean pageBean = replyService.getPageBean(pageNum, pageSize,
		// hql,hqlCount,
		// parameters);
		// ActionContext.getContext().getValueStack().push(pageBean);
		// String hql = "FROM Reply r WHERE r.topic=? ORDER BY r.postTime ASC";
		// List<Object> parameters = new ArrayList<Object>();
		// parameters.add(topic);
		//
		// PageBean pageBean = replyService.getPageBean(pageNum, pageSize, hql,
		// parameters);
		// ActionContext.getContext().getValueStack().push(pageBean);

		// 准备分页信息, 最终版
		new QueryHelper(Reply.class, "r")//
				.addCondition("r.topic=?", topic)//
				.addOrderProperty("r.postTime", true)//
				.preparePageBean(replyService, pageNum, pageSize);

		return "show";
	}

	public String addUI() throws Exception {
		Repository repository = repositoryService.getById(repositoryId);
		ActionContext.getContext().put("repository", repository);
		return "addUI";
	}

	public String delete() throws Exception{
		topicService.delete(topicId);
		return "toList";
		
	}
	


	public String add() throws Exception {
		// 表单中参数传递过来,已经封装了
		// model.setTitle(title);
		// model.setContent(content);
		model.setRepository(repositoryService.getById(repositoryId));

		model.setAuthor(getCurrentUser());// 当前登录用户
		model.setIpAddr(ServletActionContext.getRequest().getRemoteAddr());// 当前请求中的IP地址
		model.setPostTime(new Date());// 当前时间

		topicService.save(model);

		return "toShow";
	}

	public Long getRepositoryId() {
		return repositoryId;
	}

	public void setRepositoryId(Long repositoryId) {
		this.repositoryId = repositoryId;
	}

}
